package com.frogs.ucenter.export;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.frogs.framework.component.cache.Cache;
import com.frogs.system.util.GoodsEvaluateEnum;
import com.frogs.ucenter.util.DataCacheKey;

@Component
public class CleanEvaluateCache {
	
	 @Resource(name="xmemcached")
	 public Cache cache;

	 @Async
	 public void cleanEvaluateCache(Integer userId , List<Integer> goodsIds ){
		if (userId != null) {
			this.cleanEvaluateCacheByUserId(userId);
		}
		if (goodsIds != null) {
			this.cleanEvaluateCacheByGoodsIds(goodsIds);
		}
	 }
	
	 private void cleanEvaluateCacheByUserId(Integer userId) {
		StringBuilder mapKey = new StringBuilder();
		List<Integer> sortList = GoodsEvaluateEnum.getSortList();
		for (Integer id : sortList) {
			mapKey.setLength(0);
			mapKey.append(DataCacheKey.goodsEvaluateMapKey);
			mapKey.append("_USER_").append(userId);
			mapKey.append("_SORT_").append(id);
			cache.remove(mapKey.toString());
		}
		
	 }
	
	 private void cleanEvaluateCacheByGoodsIds(List<Integer> goodsIds ){
		StringBuilder mapKey = new StringBuilder();
		if (goodsIds != null) {
			for (int i=0; i<goodsIds.size(); i++) {
			    
				List<Integer> sortList = GoodsEvaluateEnum.getSortList();
				for (Integer id : sortList) {
					mapKey.setLength(0);
					mapKey.append(DataCacheKey.goodsEvaluateMapKey);
					mapKey.append("_GOODS_").append(goodsIds.get(i));
					mapKey.append("_SORT_").append(id);
					cache.remove(mapKey.toString());
				}
			}
		}
	 }
}
